package com.xrj4j.car_news.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnClickListener
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.xrj4j.car_news.BrandListActivity
import com.xrj4j.car_news.R
import com.xrj4j.car_news.databinding.ItemLogoBinding
import com.xrj4j.car_news.model.LogoItem
import com.xrj4j.car_news.model.ResultDto

class LogoAdapter(val ctx: Context, private var dataList: MutableList<LogoItem>) :
    RecyclerView.Adapter<LogoAdapter.LogoHolder>() {

    private val inflater = LayoutInflater.from(ctx)

    class LogoHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var binding: ItemLogoBinding? = null
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LogoHolder {
        val binding: ItemLogoBinding =
            DataBindingUtil.inflate(inflater, R.layout.item_logo, parent, false)
        val holder = LogoHolder(binding.root)
        holder.binding = binding
        return holder
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: LogoHolder, position: Int) {
        val data = dataList[position]
        holder.binding?.item = data

        // 给每项添加点击事件
        holder.binding?.root?.setOnClickListener {
            val intent = Intent(ctx, BrandListActivity::class.java)
            intent.putExtra("data", data)
            ctx.startActivity(intent)
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun addDataList(newDataList: List<LogoItem>?) {
        if (newDataList != null) {
            dataList.clear()
            dataList.addAll(newDataList)
            this.notifyDataSetChanged()
        }
    }

}